<?php
require_once (HOME . '/include/base/BackendController.php');
class departmentManager extends BackendController {
	/**
	 */
	private $mod_department;
	function __construct() {
		parent::BackendController ();
		
		$this->departmentManager ();
	}
	function departmentManager() {
		$this->mod_department = spClass ( 'm_department' );
	}
	function index() {
		$result = $this->mod_department->spLinker ()->findAll ();
		
// 		dump ( $result );
		
		$this->assign ( 'departmentList', $result );
			$this->display ( 'departmentManager.list.layout.html' );
	}
	function add() {
		if (!IS_POST){//没有POST提交数据
			$this->assign ( 'action', 'list' );
			$this->display ( 'departmentManager.list.layout.html' );
		}else if(IS_POST){//普通POST 提交
			$dep_id = $this->spArgs('id');
			$is_allow = $this->spArgs('is_allow');
			if($dep_id && $is_allow){
				$this->mod_department->spLinker()->create();
			}
		}else if(IS_AJAX){//AJAX提交
			
		}else {
			//Do Nothing
		}
	}
	function edit() {
	}
	function del() {
	}
	function ajax_tree() {
		$tree = array ();
		if ($dep_id = $this->spArgs ( 'dep_id' )) {
			$result = $this->mod_department->spLinker ()->findAll ( "parent_id = {$dep_id}" );
// 			dump ( $result );
// 			json_encode($result);
		} else {
			$result = $this->mod_department->spLinker ()->findAll ( 'parent_id = 0' );
		} // sql 查询
		if (empty ( $result )) {
			echo json_encode ( array (
					'error' => '下面没有数据了!!!' 
			) );
		} else {
			foreach ( $result as $i => $dep_obj ) {
				$tree [$i] ['id'] = $dep_obj ['department_id'];
				$tree [$i] ['name'] = $dep_obj ['name'];
				$tree [$i] ['dep_name'] = $dep_obj ['name'];
				$tree [$i] ['isParent'] = TRUE;
				$tree [$i] ['department_id'] = "my HERO";
			}
		echo json_encode ( $tree );
		}
// 		var_dump ( $tree );
		
		// dump ( $result );
	}
}
